﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace DiceGame
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Instance List Dice
                List<Dice> lstDice = new List<Dice>();

                // Read file
                var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly();
                string path = Path.Combine(Path.GetDirectoryName(assembly.Location), "ConfigsDice.txt");
                using (StreamReader myFile = new StreamReader(path))
                {
                    while(!myFile.EndOfStream)
                    {
                        string[] currentLine = myFile.ReadLine().Split(',');
                        string diceName = currentLine[0].Split(':')[1];
                        int favoredFace = int.Parse(currentLine[1].Split(':')[1]);
                        int proportionApply = int.Parse(currentLine[2].Split(':')[1]);
                        // Create new Dice Object and Add it to list Dice
                        lstDice.Add(new Dice(diceName, favoredFace, proportionApply));
                    }
                }

                // Input number of times the pair should be rolled
                int numberOfTimes = 0;
                do
                {
                    Console.WriteLine("Please input number of times the pair should be rolled:");
                    numberOfTimes = int.Parse(Console.ReadLine());
                } while (numberOfTimes <= 0);
                
                // Show Setting of earch Die
                Console.WriteLine("----------------------------------------------");
                lstDice.ForEach(x => { DisplayDiceSetting(x); });
                Console.WriteLine("----------------------------------------------");
                
                // perform rolling dice and show result
                for (int i = 0; i < numberOfTimes; i++)
                {
                    Console.WriteLine("---Start Rolling dice: Time {0}", i + 1);
                    lstDice.ForEach(x=> 
                                    {
                                        int currentFace = x.RollingDice();
                                        Console.WriteLine(":::{0}----Current Face:{1}", x.Name, currentFace);
                                    });
                    Console.WriteLine();
                }

                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                Console.ReadKey();
            }
        }

        /// <summary>
        /// Display settings of dice 
        /// </summary>
        /// <param name="die"> type of Dice object</param>
        private static void DisplayDiceSetting(Dice die)
        {
            Console.WriteLine("Name: {0}", die.Name);
            Console.WriteLine("The favoredFace: {0}", die.FavoredFace);
            Console.WriteLine("The ProportionApply: {0}", die.ProportionApply);
            Console.WriteLine();
        }
    }
}
